package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.storageRecord.StorageRecordService;
import cn.cvs.service.supplier.SupplierService;
import cn.cvs.service.sysRole.SysRoleService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 许欣
 * 时间：2022/4/30 15:31
 * 描述：
 */
@Controller
@RequestMapping(value = "/storage")
public class StorageRecordController {
    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;
    //查询列表
    @RequestMapping(value = "/list")
    public String list(Model model,String supName, Integer supplierId,Integer payStatus
            ,@RequestParam(defaultValue = "1")Integer pageIndex) throws Exception {
        List<StorageRecord>storageRecordList=null;
        try {
            //设置页面容量
            int pageSize = Constants.pageSize;
            //总数量
            int totalCount = storageRecordService.getCount(supName, supplierId,payStatus);
            //总页数
            PageSupport pageSupport = new PageSupport();
            pageSupport.setCurrentPageNo(pageIndex);
            pageSupport.setPageSize(pageSize);
            pageSupport.setTotalCount(totalCount);
            int totalPageCount = pageSupport.getTotalPageCount();
            //控制首页和尾页
            if (pageIndex > totalPageCount) {
                pageIndex = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            storageRecordList = storageRecordService.getList(supName, supplierId, payStatus, pageIndex, pageSize);
            model.addAttribute("storageRecordList", storageRecordList);
            List<Supplier> supplierList = null;
            supplierList = supplierService.getList();
            model.addAttribute("supplierList", supplierList);
            model.addAttribute("supName", supName);
            model.addAttribute("supplierId", supplierId);
            model.addAttribute("payStatus",payStatus);
            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("pageIndex", pageIndex);
        }catch (Exception e){
            e.printStackTrace();
//            return "redirect:/user/toSysError";
        }
        return "/storageRecord/list";
    }
    //添加
    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "/storageRecord/add";
    }
    @RequestMapping(value = "/add")
    public String add(StorageRecord storageRecord) throws Exception {
        storageRecordService.add(storageRecord);
        return "redirect:/storageRecord/list";
    }
    //修改
    @RequestMapping(value = "/toUpdate")
    public String toUpdate(@RequestParam Integer id,Model model) throws Exception {
        StorageRecord storageRecord=storageRecordService.getById(id);
        model.addAttribute(storageRecord);
        return "/storageRecord/update";
    }
    @RequestMapping(value = "/update")
    public String update(StorageRecord storageRecord) throws Exception {
        storageRecordService.update(storageRecord);
        return "redirect:/storageRecord/list";
    }
    //删除
    @RequestMapping(value = "/del/{id}")
    public String del(@PathVariable Integer id) throws Exception {
        storageRecordService.deleteById(id);
        return "redirect:/storageRecord/list";
    }
    //根据id获取供货单
    @RequestMapping(value = "/view/{id}")
    public String view(@PathVariable Integer id,Model model) throws Exception {
      StorageRecord storageRecord=  storageRecordService.getById(id);
        model.addAttribute(storageRecord);
        return "/storageRecord/view";
    }
}
